<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../../../layui-v2.6.8/layui-v2.6.8/layui/css/layui.css">
    <style>
        .box{
            width: 600px;
            margin: 0 auto;
            border: 1px solid gold;
        }
        .box .item{
            padding: 10px;
            border: 1px solid rebeccapurple;
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
    
    <div class="box">
        <div class="layui-card">
            <div class="layui-card-header">卡片面板</div>
            <div class="layui-card-body list">
              <!-- <div class="item">
                  <input type="checkbox">
                  <a href="javascript:;">hhahahaha</a>
              </div> -->
              
            </div>
          </div>
          <form class="layui-form" action="">
            <div class="layui-form-item">
              <label class="layui-form-label">输入框</label>
              <div class="layui-input-block">
                <input class="input" type="text" name="content" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
              </div>
            </div>
            
           
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
             
              </div>
            </div>
          </form>
    </div>
   

</body>
<script src="../../../layui-v2.6.8/layui-v2.6.8/layui/layui.js"></script>
<script src="../../../jQuery/jquery.js"></script>
<script>
    $(function(){
       const mylist = function(){
        $.ajax({
            type:'get',
            url:'https://api.yizhanketang.cn/api/v1/todos',
            success(res){
                // console.log(res)
                const {data} = res
                // console.log(data)
                data.forEach(item =>{
                    // console.log(item)
                    const{completed,content:title} = item
                    $(`<div class="item">
                  <input type="checkbox" ${completed ? 'checked' : ''}>
                  <a href="javascript:;">${title}</a>
              </div>`).appendTo('.list')
                })
                
            }
        })
       }
       mylist()


        layui.use('form', function(){
        const form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
            $.ajax({
                type:'post',
                url:'https://api.yizhanketang.cn/api/v1/todos',
                data:data.field,
                success(res){
               $('.list').empty()
               mylist()
               $('.input').val('')
                },
                error(e){
                  layer.msg('不能为空');  
                }  
            })
            return false
            
        })
        })
            })
</script>
</html>